package com.snail.snailvr.views;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;

import com.snail.snailvr.R;
import com.snail.snailvr.utils.Constant;
import com.snail.snailvr.utils.LockUtil;
import com.snail.snailvr.utils.LogUtils;
import com.snail.snailvr.utils.NetworkUtil;
import com.snail.snailvr.utils.SharedPreferencesUtil;
import com.snail.snailvr.utils.SmartBarUtils;
import com.snail.snailvr.utils.ToastUtils;
import com.snailgame.fastdev.FastDevActivity;

/**
 * Created by snail on 16/3/28.
 */
public abstract class BaseActivity extends FastDevActivity {
    private static boolean isLeave = false;
    private static int lastNetworkType = NetworkUtil.TYPE_NOT_CONNECTED;
    private static Activity mCurrentActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SmartBarUtils.hide(getWindow().getDecorView());
        mCurrentActivity=this;
    }

    @Override
    protected void loadData() {
    }

    @Override
    protected void onUserLeaveHint() {//当用户按Home键等操作使程序进入后台时即开始计时
        super.onUserLeaveHint();
        if (!isLeave) {
            isLeave = true;
            saveStartTime();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterBroadcastReceiver();
    }

    @Override
    protected void onResume() {//当用户使程序恢复为前台显示时执行onResume()方法,判断是否超时
        super.onResume();
        mCurrentActivity=this;
        registerBroadcastReceiver();
        if (isLeave) {
            isLeave = false;
            timeOutCheck();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void timeOutCheck() {
        long endtime = System.currentTimeMillis();
        LogUtils.d("endtime  == " + endtime);
        LogUtils.d("starttime  == " + getStartTime());
        LogUtils.d("endtime - getStartTime == " + (endtime - getStartTime()));

        if (endtime - getStartTime() >= 15 * 60 * 1000) {
            //超时
            if (LockUtil.getPwd(this).length >= Constant.PATTERN_PSW_MIN_LENGTH) {
                Intent intent = GestureUnlockActivity.getIntent(this, false);
                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
            }
        }
    }

    private void saveStartTime() {
        SharedPreferencesUtil.getInstance(this).setStartTime();
    }

    private long getStartTime() {

        return SharedPreferencesUtil.getInstance(this).getStartTime();
    }


    private BroadcastReceiver wifiReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (NetworkUtil.isMobileDataConnected()) {
                if (lastNetworkType != NetworkUtil.TYPE_MOBILE) {
                    lastNetworkType = NetworkUtil.TYPE_MOBILE;
                    ToastUtils.makeToastColorShow(BaseActivity.this, findViewById(android.R.id.content), getString(R.string.use_mobile_data));
                    wifiDisconnected();
                }

            } else if (NetworkUtil.isWifiConnected()) {
                if (lastNetworkType != NetworkUtil.TYPE_WIFI) {
                    lastNetworkType = NetworkUtil.TYPE_WIFI;
                    wifiConnected();
                }
            } else {
                if (lastNetworkType != NetworkUtil.TYPE_NOT_CONNECTED) {
                    wifiDisconnected();
                    lastNetworkType = NetworkUtil.TYPE_NOT_CONNECTED;
                }
            }

        }
    };

    // 动态注册广播
    private void registerBroadcastReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(wifiReceiver, filter);
    }

    // 解除广播
    private void unregisterBroadcastReceiver() {
        if (wifiReceiver != null) {
            unregisterReceiver(wifiReceiver);
        }
    }

    /**
     * Wifi断开连接
     */
    protected void wifiDisconnected() {
    }

    /**
     * Wifi已连接
     */
    protected void wifiConnected() {
    }

    public static Activity getCurrentActivity() {
        return mCurrentActivity;
    }
}